package com.smart.bookshelf

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.CountDownTimer
import android.util.Log
import kotlin.random.Random

class BootCompleteReceiver : BroadcastReceiver() {
    //    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    override fun onReceive(context: Context, intent: Intent) {
        if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
//            Intent thisIntent = new Intent(context, MainActivity.class);
//            thisIntent.setAction("android.intent.action.MAIN");
//            thisIntent.addCategory("android.intent.category.LAUNCHER");
//            thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(thisIntent);

            object : CountDownTimer(Random.nextLong(30, 120)*1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                  Log.v("BootCompleteReceiver",millisUntilFinished.toString())
                }

                override fun onFinish() {
                    val packageManager = context.packageManager
                    val mainIntent = packageManager.getLaunchIntentForPackage("com.smart.bookshelf")
                    mainIntent!!.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    context.startActivity(mainIntent)
                }
            }.start()

        }
    }
}

